package com.zzh.custom.chess;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Region;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.zzh.tools.system.ToolScreen;

public class ChessBoardView extends View {

    private static final String TAG = ChessBoardView.class.getSimpleName();

    public interface OnPointClickListener {
        void onPointClick(int key, PointF pointF);
    }


    public ChessBoardView(Context context) {
        this(context, null);
    }

    public ChessBoardView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ChessBoardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mDip2px1 = ToolScreen.dip2px(context, 1);
        createPaint();
    }

    private void createPaint() {
        mPaintLine.setStrokeWidth(2f);
        mPaintLine.setAntiAlias(true);
        mPaintLine.setColor(Color.BLACK);
        mPaintLine.setTextSize(mDip2px1 * 10);
    }

    // 所有的交叉点
    private SparseArray<PointF> mCrossPoints = new SparseArray<>(90);
    private SparseArray<Region> mCrossRegions = new SparseArray<>(90);
    // 标记为集合
    private int[] mTagIndexs = new int[]{
            203, 803, 104, 304, 504, 704, 904, 107, 307, 507, 707, 907, 208, 808
    };
    // 1dp对应的像素值
    private int mDip2px1;
    // 单段的长度
    private float mDeviationValue;
    // 点击回掉事件
    private OnPointClickListener mOnPointClickListener;

    private int mWidth;
    private int mHeight;

    // 绘制的paint
    private Paint mPaintLine = new Paint();

    private void updateCressPoint() {
        int width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
        if (width == 0) return;
        int height = getMeasuredHeight() - getPaddingBottom() - getPaddingTop();
        if (height == 0) return;
        if (mWidth == width && mHeight == height) {
            return;
        }
        mWidth = width;
        mHeight = height;
        mDeviationValue = ((float) (width > height ? height : width)) / 9;
        float startY = getPaddingTop() + mDeviationValue / 2;
        float startX = width / 2f - mDeviationValue * 4;
        float halfDeviationValue = mDeviationValue / 3;
        for (int v = 1; v < 11; v++) {
            // 竖向变化
            float y = startY + mDeviationValue * (v - 1);
            for (int h = 1; h < 10; h++) {
                // 横向变化
                float x = startX + mDeviationValue * (h - 1);
                int key = h * 100 + v;
                // 处理点的问题
                PointF pointF = mCrossPoints.get(key);
                if (pointF == null) {
                    pointF = new PointF();
                    mCrossPoints.put(key, pointF);
                }
                pointF.set(x, y);
                // 处理点击位置的处理
                Region region = mCrossRegions.get(key);
                if (region == null) {
                    region = new Region();
                    mCrossRegions.put(key, region);
                } else {
                    region.setEmpty();
                }
                region.set((int) (x - halfDeviationValue), (int) (y - halfDeviationValue),
                        (int) (x + halfDeviationValue), (int) (y + halfDeviationValue));
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mWidth <= 0) return;
        // 绘制横线
        for (int i = 1; i < 11; i++) {
            PointF startPointF = mCrossPoints.get(100 + i);
            PointF endPointF = mCrossPoints.get(9 * 100 + i);
            canvas.drawLine(startPointF.x, startPointF.y, endPointF.x, endPointF.y, mPaintLine);
            // 绘制竖向数据点 todo
            canvas.drawText(String.valueOf(i), startPointF.x - mDip2px1 * 10, startPointF.y, mPaintLine);
        }
        // 绘制竖线
        for (int i = 1; i < 10; i++) {
            PointF startPointF = mCrossPoints.get(i * 100 + 1);
            PointF centerOnePointF = mCrossPoints.get(i * 100 + 5);
            PointF centerTwoPointF = mCrossPoints.get(i * 100 + 6);
            PointF endPointF = mCrossPoints.get(i * 100 + 10);
            if (i == 1 || i == 9) {
                canvas.drawLine(centerOnePointF.x, centerOnePointF.y, centerTwoPointF.x, centerTwoPointF.y, mPaintLine);
            }
            canvas.drawLine(startPointF.x, startPointF.y, centerOnePointF.x, centerOnePointF.y, mPaintLine);
            canvas.drawLine(centerTwoPointF.x, centerTwoPointF.y, endPointF.x, endPointF.y, mPaintLine);
            // 绘制横向数据点 todo
            canvas.drawText(String.valueOf(i), startPointF.x, startPointF.y - mDip2px1 * 3, mPaintLine);
        }
        // 绘制标记位
        float tagLength = mDeviationValue / 6;
        float tagInterval = tagLength / 3;
        for (int i = 0; i < mTagIndexs.length; i++) {
            int index = mTagIndexs[i];
            PointF pointF = mCrossPoints.get(index);
            int coordinatesX = index / 100;
            if (coordinatesX != 9) {
                // 绘制一象限
                canvas.drawLine(pointF.x + tagInterval, pointF.y - tagInterval - tagLength,
                        pointF.x + tagInterval, pointF.y - tagInterval, mPaintLine);
                canvas.drawLine(pointF.x + tagInterval, pointF.y - tagInterval,
                        pointF.x + tagInterval + tagLength, pointF.y - tagInterval, mPaintLine);
                // 绘制二象限
                canvas.drawLine(pointF.x + tagInterval, pointF.y + tagInterval,
                        pointF.x + tagInterval, pointF.y + tagInterval + tagLength, mPaintLine);
                canvas.drawLine(pointF.x + tagInterval, pointF.y + tagInterval,
                        pointF.x + tagInterval + tagLength, pointF.y + tagInterval, mPaintLine);
            }
            if (coordinatesX != 1) {
                // 绘制三象限
                canvas.drawLine(pointF.x - tagInterval - tagLength, pointF.y + tagInterval,
                        pointF.x - tagInterval, pointF.y + tagInterval, mPaintLine);
                canvas.drawLine(pointF.x - tagInterval, pointF.y + tagInterval,
                        pointF.x - tagInterval, pointF.y + tagInterval + tagLength, mPaintLine);
                // 绘制四象限
                canvas.drawLine(pointF.x - tagInterval, pointF.y - tagInterval - tagLength,
                        pointF.x - tagInterval, pointF.y - tagInterval, mPaintLine);
                canvas.drawLine(pointF.x - tagInterval - tagLength, pointF.y - tagInterval,
                        pointF.x - tagInterval, pointF.y - tagInterval, mPaintLine);
            }
        }
        // 绘制将 士 空间
        PointF startPoint;
        PointF endPoint;
        startPoint = mCrossPoints.get(401);
        endPoint = mCrossPoints.get(603);
        canvas.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, mPaintLine);
        startPoint = mCrossPoints.get(601);
        endPoint = mCrossPoints.get(403);
        canvas.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, mPaintLine);
        startPoint = mCrossPoints.get(408);
        endPoint = mCrossPoints.get(610);
        canvas.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, mPaintLine);
        startPoint = mCrossPoints.get(410);
        endPoint = mCrossPoints.get(608);
        canvas.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, mPaintLine);
    }

    // 是不是允许点击
    private boolean mIsAllowClick = true;
    // 按下的坐标值
    private int mDownX;
    private int mDownY;
    // 按下的key值
    private int mDownKey;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!mIsAllowClick) {
            return super.onTouchEvent(event);
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownX = (int) event.getX();
                mDownY = (int) event.getY();
                for (int i = 0; i < mCrossRegions.size(); i++) {
                    Region region = mCrossRegions.valueAt(i);
                    if (region.contains(mDownX, mDownY)) {
                        mDownKey = mCrossRegions.keyAt(i);
                        return true;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                float nowX = event.getX();
                float nowY = event.getY();
                float halfDeviationValue = mDeviationValue / 3;
                if (Math.abs(mDownX - nowX) > halfDeviationValue || Math.abs(mDownY - nowY) > halfDeviationValue) {
                    return super.onTouchEvent(event);
                }
                if (mOnPointClickListener != null) {
                    mOnPointClickListener.onPointClick(mDownKey, mCrossPoints.get(mDownKey));
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    public void setOnPointClickListener(OnPointClickListener onPointClickListener) {
        this.mOnPointClickListener = onPointClickListener;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        updateCressPoint();
    }

    public PointF getSpecifiedPointF(int key) {
        return mCrossPoints.get(key);
    }

}
